<template>
  <div>
    <el-card style="margin: 20px 0"
      ><CategorySelect
        @getCategoryId="getCategoryId"
        :show="scene != 0"
      ></CategorySelect
    ></el-card>
    <el-card>
      <!--展示spu列表的结构-->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!c3Id"
          @click="addSpu"
          >添加SPU</el-button
        >
        <el-table style="width: 100%; margin: 10px 0" border :data="records">
          <el-table-column
            type="index"
            label="序号"
            width="80px"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="spuName" label="spu名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="spu描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <a title="添加sku" class="buttons"
                ><el-button
                  type="success"
                  icon="el-icon-plus"
                  size="mini"
                  @click="addSku(row)"
                ></el-button
              ></a>
              <a title="修改spu" class="buttons"
                ><el-button
                  type="warning"
                  icon="el-icon-edit"
                  size="mini"
                  @click="updateSpu(row)"
                ></el-button
              ></a>
              <a title="查看当前spu全部sku列表" class="buttons"
                ><el-button
                  type="info"
                  icon="el-icon-info"
                  size="mini"
                  @click="handler(row)"
                ></el-button
              ></a>
              <el-popconfirm title="确定删除吗？" @onConfirm="deleteSpu(row)">
                <a title="删除spu" class="buttons" slot="reference"
                  ><el-button
                    type="danger"
                    icon="el-icon-delete"
                    size="mini"
                    slot="reference"
                  ></el-button
                ></a>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="page"
          :page-sizes="[3, 5, 10]"
          :page-size="limit"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          style="text-align: center"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>
      <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spu" />
      <SkuForm v-show="scene == 2" ref="sku" @changeScenes="changeScenes" />
    </el-card>
    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
      :before-close="close"
    >
      <el-table :data="skuList" style="width: 100%" border v-loading="loading">
        <el-table-column prop="skuName" label="名称" width="width">
        </el-table-column>
        <el-table-column prop="price" label="价格" width="width">
        </el-table-column
        ><el-table-column prop="weight" label="重量" width="width">
        </el-table-column
        ><el-table-column label="默认图片" width="width">
          <template slot-scope="{ row, $index }">
            <img
              :src="row.skuDefaultImg"
              alt="图片加载失败"
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SpuForm from "./SpuForm";
import SkuForm from "./SkuForm";
export default {
  name: "Spu",
  data() {
    return {
      c1Id: "",
      c2Id: "",
      c3Id: "",
      page: 1, //当前第几页
      limit: 3, //每一页需要展示多少数据
      records: [], //存储spu列表的数组
      total: 0, //分页器一共需要展示数据的条数
      scene: 0, //0代表展示spu列表数据，1代表添加或需修改spu，2代表添加sku
      dialogTableVisible: false, //控制对话框显示与隐藏
      spu: {},
      skuList: [],
      loading: true,
    };
  },
  methods: {
    //三级联动自定义事件，把子组件相应的id传递给父组件
    getCategoryId({ cId, level }) {
      if (level == 1) {
        this.c1Id = cId;
        this.c2Id = "";
        this.c3Id = "";
      } else if (level == 2) {
        this.c2Id = cId;
        this.c3Id = "";
      } else {
        this.c3Id = cId;
        this.getSpuList();
      }
    },
    //获取spu列表数据
    async getSpuList() {
      const { limit, page, c3Id } = this;
      let result = await this.$API.spu.reqSpuList(page, limit, c3Id);
      if (result.code == 200) {
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },
    //点击分页器第几页按钮回调函数
    handleCurrentChange(page) {
      this.page = page;
      this.getSpuList();
    },
    //分页器一页展示数据条数变化时的回调
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    //添加spu
    addSpu() {
      this.scene = 1;
      //通知子组件发请求
      this.$refs.spu.addSpuData(this.c3Id);
    },
    //修改某个spu
    updateSpu(row) {
      this.scene = 1;
      this.$refs.spu.initSpuData(row);
    },
    //spuForm自定义事件回调,切换场景
    changeScene({ scene, flag }) {
      this.scene = scene;
      if (flag == "修改") {
        this.getSpuList();
      } else if (flag == "添加") {
        this.page = 1;
        this.getSpuList();
      }
    },
    //删除spu按钮回调函数
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id);
      if (result.code == 200) {
        this.$message({ type: "success", message: "删除成功" });
        //如果当前只剩一个可删除的选项了，那么删除之后需要到上一页去
        if (this.records.length == 1) {
          this.page = this.page - 1;
        }
        this.getSpuList();
      }
    },
    //添加sku按钮的回调
    addSku(row) {
      this.scene = 2;
      //让sku组件发请求
      this.$refs.sku.getData(this.c1Id, this.c2Id, row);
    },
    //skuForm通知父组件回到场景0的回调
    changeScenes(scene) {
      this.scene = scene;
    },
    //查看当前SKU按钮回调
    async handler(spu) {
      this.dialogTableVisible = true;
      this.spu = spu;
      //获取sku列表数据
      let result = await this.$API.spu.reqSkuList(spu.id);
      if (result.code == 200) {
        this.skuList = result.data;
        this.loading = false;
      }
    },
    //关闭对话框
    close(done) {
      this.loading = true;
      this.skuList = [];
      //关闭对话框
      done();
    },
  },
  components: {
    SkuForm,
    SpuForm,
  },
};
</script>

<style lang="css" scoped>
.buttons {
  margin: 0 5px;
}
</style>
